﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
using System.Data.OleDb;

namespace DAO
{
    public class PhieuThueDAO: AbtractDAO
    {
        public static List<PhieuThueDTO> layDSPhieuThue()
        {
            OleDbConnection ketNoi = null;
            List<PhieuThueDTO> dsPhieuThue = new List<PhieuThueDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaPhieuThue,TenPhieuThue,PT.MaPhong,NgayBatDauThue FROM PHIEUTHUE PT, PHONG P WHERE PT.MaPhong=P.MaPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    PhieuThueDTO PhieuThue = new PhieuThueDTO();
                    PhieuThue.MaPhieuThue = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        PhieuThue.TenPhieuThue = boLoc.GetString(1);
                    }
                    PhongDTO p = new PhongDTO();

                    p.MaPhong = boLoc.GetInt32(2);

                    PhieuThue.Phong = p;

                    if (!boLoc.IsDBNull(3))
                    {
                        PhieuThue.NgayBatDauThue = boLoc.GetDateTime(3);
                    }
                    dsPhieuThue.Add(PhieuThue);
                }
            }
            catch (Exception ex)
            {
                dsPhieuThue = new List<PhieuThueDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsPhieuThue;
        }

        public static void themPhieuThue(long maPhong, DateTime ngayBatDauThue)
        {
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO PHIEUTHUE(MaPhong,NgayBatDauThue) VALUES(@MaPhong,@NgayBatDauThue)";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@MaPhong", OleDbType.Integer);
                thamSo.Value = maPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@NgayBatDauThue", OleDbType.Date);
                thamSo.Value = ngayBatDauThue;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }
        public static long layMaPTTheoMaPhongvaNgayBDThue(string maphong,DateTime ngaylap)
        {
            OleDbConnection ketNoi = null;
            long maPT = 0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaPhieuThue FROM PHIEUTHUE WHERE MaPhong=@MaPhong and NgayBatDauThue= @NgayLap";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo2 = new OleDbParameter("@MaPhong", OleDbType.Char);
                thamSo2.Value = maphong;

                lenh.Parameters.Add(thamSo2);

                thamSo2 = new OleDbParameter("@NgayLap", OleDbType.Date);
                thamSo2.Value = ngaylap.ToString();
                lenh.Parameters.Add(thamSo2);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                if (boLoc.Read())
                {
                    maPT = boLoc.GetInt32(0);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return maPT;
        }
        public static void updatePhieuThuetheoNgayThue(int ngaythue,string maPhong,string ngaybd)
        {
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "update PHIEUTHUE set SoNgayThue = @SoNgayThue where MaPhong = @MaPhong and NgayBatDauThue = @NgayBD";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@SoNgayThue", OleDbType.Integer);
                thamSo.Value = ngaythue;
                lenh.Parameters.Add(thamSo);
                thamSo = new OleDbParameter("@MaPhong", OleDbType.Integer);
                thamSo.Value = maPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@NgayBatDauThue", OleDbType.Date);
                thamSo.Value = ngaybd;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }
    }
}
